//
//  DZZDestinationViewController.m
//  DZZTravel
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "DZZDestinationViewController.h"

@interface DZZDestinationViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,upAndDownDelegate,jumpDelegate>

@property (nonatomic ,strong)HeadImageView *headView;
//图片
@property (nonatomic ,strong)UICollectionView *hotCity;
//汉字
@property (nonatomic ,strong)UICollectionView *hotSince;

@property (nonatomic ,strong)downMenu *Menu;
//数据数组
@property (nonatomic ,strong)NSMutableArray *hotSinceArray;
@property (nonatomic ,strong)NSMutableArray *hotCityArray;


@end

@implementation DZZDestinationViewController


- (downMenu *)Menu{
    if (!_Menu) {
        _Menu = [[downMenu alloc]initWithFrame:CGRectMake(0, kheight/4+64, kWidth,kheight-kheight/4-64-44)];
        _Menu.backgroundColor = [UIColor orangeColor];
        _Menu.delegate = self;
        _Menu.hidden = YES;
    }
    return _Menu;
}

- (NSMutableArray *)hotSinceArray{
    if (!_hotSinceArray) {
        _hotSinceArray = [NSMutableArray array];
    }
    return _hotSinceArray;
}

- (NSMutableArray *)hotCityArray{
    if (!_hotCityArray) {
        _hotCityArray = [NSMutableArray array];
    }
    return _hotCityArray;
}

- (HeadImageView *)headView{
    if (!_headView) {
        _headView = [[HeadImageView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kheight/4)];
        _headView.delelgate = self;
        
        
    }
    return _headView;
}

- (UICollectionView *)hotCity{
    if (!_hotCity) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(kWidth/2-30, kWidth/2-80);
        layout.sectionInset = UIEdgeInsetsMake(10, 20, 0, 20);
        _hotCity = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kheight/4+64, kWidth, 1.5*kheight/4) collectionViewLayout:layout];
        _hotCity.backgroundColor = [UIColor clearColor];
        _hotCity.scrollEnabled = YES;
    }
    return _hotCity;
}

- (UICollectionView *)hotSince{
    if (!_hotSince) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(50, 50);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        
        _hotSince = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kheight-kheight/4, kWidth, kheight/6) collectionViewLayout:layout];
        _hotSince.scrollEnabled = YES;
        _hotSince.backgroundColor = [UIColor clearColor];
    }
    return _hotSince;
}
//添加子视图
- (void)addSubView{
    
    [self.view addSubview:self.headView];
    [self.view addSubview:self.hotCity];
    [self.view addSubview:self.hotSince];
    [self.view addSubview:self.Menu];
    
   }



#pragma mark ---------------下拉菜单的代理---------


- (void)upAndDown{
   
    
self.Menu.hidden = !self.Menu.hidden;
    
}
//下拉菜单选则城市
- (void)jumpWithCityNmae:(cityName *)city{
    if ([city.name isEqualToString:@"热门"]) {
       
    }
    [self jump:city];
}

//更改热门显示方式

- (void)changeHto{
    self.hotSince.hidden = YES;
    self.hotCity.frame = self.view.frame;
    
}

////slider选择城市
- (void)jump:(cityName *)city{
    if ([city.name isEqualToString:@"热门"]) {
        
    }
    
    [self.hotSinceArray removeAllObjects];
    [self.hotCityArray removeAllObjects];
    self.Menu.hidden = YES;
    NSString *urlCity = [NSString stringWithFormat:@"http://apiphp.yaochufa.com/portal/dest/scenicCity?areaCode=%@&system=ios&channel=AppStore&listType=province&imei=3BB694A0-037E-",city.areaCode];
    
    
    [RequestManager fectDataWithUrl:urlCity parameter:@{} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dicc in dic[@"content"][@"hotCity"]) {
            menuCity *MenuCity = [menuCity new];
            [MenuCity setValuesForKeysWithDictionary:dicc];
            [self.hotCityArray addObject:MenuCity];
            
        }
        
        for (NSDictionary *dicc in dic[@"content"][@"hotScenic"]) {
            menuCity *MenuCity = [menuCity new];
            [MenuCity setValuesForKeysWithDictionary:dicc];
            [self.hotSinceArray addObject:MenuCity];
            
        }
        
    } upDateUI:^{
       
        [self.hotCity reloadData];
        [self.hotSince reloadData];
    }];
}

//进入时加载
- (void)requestFirst{

    [RequestManager fectDataWithUrl:@"http://apiphp.yaochufa.com/portal/dest/scenicCity?areaCode=0&system=ios&channel=AppStore&listType=province&imei=3BB694A0-037E-" parameter:@{} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//      
        for (NSDictionary *dicc in dic[@"content"][@"hotCity"]) {
            cityName *city = [cityName new];
            [city setValuesForKeysWithDictionary:dicc];
            menuCity *MenuCity = [menuCity new];
            [MenuCity setValuesForKeysWithDictionary:dicc];
            
            [self.hotCityArray addObject:MenuCity];
        }
        
        for (NSDictionary *dicc in dic[@"content"][@"hotScenic"]) {
            cityName *city = [cityName new];
            [city setValuesForKeysWithDictionary:dicc];
            menuCity *MenuCity = [menuCity new];
            [MenuCity setValuesForKeysWithDictionary:dicc];
            [self.hotSinceArray addObject:MenuCity];
         
        }
        
    } upDateUI:^{
        [self.hotCity reloadData];
        [self.hotSince reloadData];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self requestFirst];
     [self.hotCity registerNib:[UINib nibWithNibName:@"HotCityCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HotCityCollectionViewCell"];
    self.hotCity.delegate = self;
    self.hotCity.dataSource = self;
    
    [self.hotSince registerNib:[UINib nibWithNibName:@"HotSinceCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HotSinceCollectionViewCell"];
    self.hotSince.delegate = self;
    self.hotSince.dataSource = self;
    
    
    [self addSubView];
    
    
  
}


#pragma mark----------------collectionView------------------

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.hotCity) {
        return self.hotSinceArray.count;
    }else
        return self.hotCityArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.hotCity) {
        HotCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCityCollectionViewCell" forIndexPath:indexPath];
        
        menuCity *city = self.hotSinceArray[indexPath.row];
        
        cell.title.text = city.scenicName;
        [cell.mudiPic sd_setImageWithURL:[NSURL URLWithString:city.mudiPic]];
        return cell;
    }else{
        
        menuCity *city = self.hotCityArray[indexPath.row];
        HotSinceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotSinceCollectionViewCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        
        cell.title.text = city.cityName;
     
        return cell;
    }
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.hotCity) {
        CityScenerySenViewController *cityVC =[CityScenerySenViewController new];
        menuCity *city = self.hotSinceArray[indexPath.row];
        cityVC.city = city;
        
        [self.navigationController pushViewController:cityVC animated:YES];
           }else{
               SearchCityViewController *searchCityVC = [SearchCityViewController new];
               [self.navigationController pushViewController:searchCityVC animated:YES];
            
        }
}




@end
